/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import Outils.Hash;
import Outils.LiaisonSqlDB;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author smng2095
 */
@WebServlet(name = "connexionAdmin", urlPatterns = {"/connexionAdmin"})
public class connexionAdmin extends HttpServlet {

   
    private LiaisonSqlDB liaisonSqlDB;
    private Hash hash;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        if ((request.getParameter("loginAdmin") == null) || ((request.getParameter("passwordAdmin")) == null) || ((request.getParameter("loginUser")) == null) || (request.getParameter("passwordUser") == null)) {
            redirectToSite(request, response, "admin.html");
        } else if (((String) (request.getParameter("loginAdmin"))).equalsIgnoreCase("") || ((String) (request.getParameter("passwordAdmin"))).equalsIgnoreCase("") || ((String) (request.getParameter("loginUser"))).equalsIgnoreCase("") || ((String) (request.getParameter("passwordUser"))).equalsIgnoreCase("")) {
            redirectToSite(request, response, "admin.html");
        } else {
            String droit = "no";
            if (!(request.getParameter("droit") == null))
            {
                droit = (String)request.getParameter("droit");
            }    
            
            hash = new Hash();
            String loginAdmin = (String) request.getParameter("loginAdmin");
            if (!(loginAdmin.equals("admin")))
            {
                loginAdmin = hash.hashMd5(loginAdmin);
            }
            String passwordAdmin = (String) (request.getParameter("passwordAdmin"));
            if (!(passwordAdmin.equals("admin")))
            {
                passwordAdmin = hash.hashMd5(passwordAdmin);
            }            
            String HashLoginUser = hash.hashMd5((String) request.getParameter("loginUser"));
            String HashPasswordUser = hash.hashMd5((String) request.getParameter("passwordUser"));         
                 
            liaisonSqlDB = new LiaisonSqlDB("localhost", "arnueldb", "arnuel", "mysqlarnuel");

            if (liaisonSqlDB.identifierAdmin(loginAdmin, passwordAdmin)) {
                if (liaisonSqlDB.creationUtilisateur(HashLoginUser, HashPasswordUser,droit)) {
                    request.setAttribute("info", "Création utilisateur effectuée");
                    redirectToSite(request, response, "index.jsp");
                } else {
                    request.setAttribute("error", "L'utilisateur n'a pas été créé");
                    redirectToSite(request, response, "admin.jsp");

                }
            } else {
                request.setAttribute("error", "Mauvais mot de passe Administrateur");
                redirectToSite(request, response, "admin.jsp");
            }
        }

    }

    private void redirectToSite(HttpServletRequest request, HttpServletResponse response, String site) throws ServletException, IOException {
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/" + site);
        dispatcher.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        processRequest(request, response);

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
